package org.wikinews.repo;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.wikinews.base.BaseRepo;
import org.wikinews.model.Tag;

@Name("tagRepo")
@Scope(ScopeType.STATELESS)
@AutoCreate
public class TagRepo extends BaseRepo<Tag> {
	
	@SuppressWarnings("unchecked")
	public List<Tag> getAll() {
		String hql = "from Tag order by id";
		return em.createQuery(hql).getResultList();
	}

	/**
	 * Localiza o post pelo identificador
	 * 
	 * @param postId
	 * @return
	 */
	public Tag getById(String tagId) {
		return em.find(Tag.class, tagId);
	}
	
	public List<SelectItem> getListTagsWithSelectItem() {
		List<SelectItem> list = new ArrayList<SelectItem>();
		for (Tag tag : getAll()) {
			list.add(new SelectItem(tag, tag.getId().toLowerCase()));
		}
		return list;
	}
}
